<?php
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/26 15:29
 */
namespace app\api\controller;
use think\Db;
use app\api\model\Task;

class TaskCore extends Base
{
    /**
     * 获取任务列表
     */
    public function getTaskList()
    {
        $input = input();
        $order = [];
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        if (empty($input['type'])){
            return $this->ajaxReturn('',-1,'未定义type');
        }
        if (!empty($input['commission'])){
            $order['task_commission'] = $input['commission'];
        }
        if (!empty($input['time'])){
            $order['created_at'] = $input['time'];
        }
        $this->checkRefresh($input['uid']);
        $page = empty($input['page']) ? 10 : $input['page'];
        $task = new Task();
        $where['task_type'] = ['=',$input['type']];
        $where['end_time'] = ['>',time()];
        $data = $task->getAll($where,$page,$order);
        return $this->ajaxReturn($data);
    }
    /**
     * 领取任务
     */
    public function addTask()
    {
        $input = input();
        if (empty($input['task_id'])){
            return $this->ajaxReturn('',-1,'未定义task_id');
        }
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        if (empty($input['content'])){
            return $this->ajaxReturn('',-1,'未定义content');
        }
        if (empty($input['commission'])){
            return $this->ajaxReturn('',-1,'未定义commission');
        }
        $this->checkRefresh($input['uid']);
        // $userInfo = db('user_account')->where('id',$input['uid'])->find();
        // if ($userInfo['level'] > 0){//每天刷新
        //     $time = strtotime(date('Y-m-d'));
        //     if ($userInfo['day_time'] < $time) {
        //         $task_num = db('member')->where('level',$userInfo['level'])->value('task_num');//可接任务数量
        //         $data['day_num'] = $task_num;
        //         $data['day_time'] = $time;
        //         db('user_account')->where('id',$input['uid'])->update($data);
        //     }
        // }
        $ytime = time()-3600*24;
        $check_task = db('task_record')->where('uid',$input['uid'])->where('task_id',$input['task_id'])->where('created_at >= '.$ytime.' and created_at <= '.time())->find();
        if($check_task){
            return $this->ajaxReturn('',-1,'本任务今天已领取过');
        }

        $sum = db('user_account')->where('id',$input['uid'])->value('day_num');
        if ($sum == 0) {
            return $this->ajaxReturn('',-1,'今天可接数量已满');
        }
        $data = [
            'uid' => $input['uid'],
            'task_id' => $input['task_id'],
            'content' => $input['content'],
            'commission' => $input['commission'],
            'status' => 1,
            'created_at' => time()
        ];
        $id = db('task_record')->insertGetId($data);
        if ($id) {
            $day_num = $sum - 1;
            db('user_account')->where('id',$input['uid'])->update(['day_num'=>$day_num]);
            $data['id'] = $id;
            return $this->ajaxReturn($data); 
        }else {
            return $this->ajaxReturn('',-1,'领取失败');
        }
    }

    /**
     * 任务记录
     */
    public function taskRecord() {
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        if(!empty($input['status'])){
            $where['status'] = $input['status'];
        }else{
            $where = '';
        }
        $page = empty($input['page']) ? 10 : $input['page'];
        $this->checkRefresh($input['uid']);
        $data = db('task_record')->where('uid',$input['uid'])->where($where)->order('id desc')->limit($page)->select();
        foreach ($data as $key => $value) {

            // $one_day = $value['created_at']+(60*60*24);
            $t = strtotime(date('Y-m-d 00:00:00'));//设置失效时间
            if($value['created_at'] < $t && $value['status'] == 1){
                $status['status'] = 3;
                db('task_record')->where('id',$value['id'])->update($status);
            }
            $data[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
            if (!empty($value['update_at'])){
                $data[$key]['update_at'] = date('Y-m-d H:i:s', $value['update_at']);
            }
        }
        return $this->ajaxReturn($data);
    }

    /**
     * 上传任务列表
     */
    public function uploadTaskList() {
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        $this->checkRefresh($input['uid']);
        $where = [];
        $where['uid'] = ['=',$input['uid']];
        $where['task_id'] = ['like','%'.$input['task_id'].'%'];
        $where['status'] = ['=',1];

        $page = empty($input['page']) ? 10 : $input['page'];

        $data = db('task_record')->where($where)->order('id desc')->limit($page)->select();
        foreach ($data as $key => $value) {
            
            $img = db('task')->where('id',$value['task_id'])->value('task_picture_url');

            if(!empty($img)){

               $data[$key]['img_array'] = explode(',',$img);

            }

            $data[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
            if (!empty($value['update_at'])){
                $data[$key]['update_at'] = date('Y-m-d H:i:s', $value['update_at']);
            }
        }
        
        return $this->ajaxReturn($data);
    }

    /**
     * 上传任务
     */
    public function uploadTask() {
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        if (empty($input['money'])){
            return $this->ajaxReturn('',-1,'未定义money');
        }
        if (empty($input['task_record_id'])){
            return $this->ajaxReturn('',-1,'未定义task_record_id');
        }
        if (empty($input['content'])){
            return $this->ajaxReturn('',-1,'未定义content');
        }
        $data = [];
        $data['uid'] = $input['uid'];
        $data['money'] = $input['money'];
        $data['task_record_id'] = $input['task_record_id'];
        $data['content'] = $input['content'];
        $data['recommend_id'] = empty($input['recommend_id']) ? '' : $input['recommend_id'];
        $data['status'] = 1;
        $data['created_at'] = time();

        //判断是否重复数据
        $check_task = db('task_record')->where('task_record_id',$data['task_record_id'])->where('uid',$data['uid'])->where('created_at',$data['created_at'])->find();

        if(empty($check_task)){

            $id = db('income')->insertGetId($data);
            if($id) {
                db('task_record')->where('uid',$input['uid'])->where('id',$input['task_record_id'])->update(['status'=>2]);
                return $this->ajaxReturn(['id'=>$id]);
            }else {
                return $this->ajaxReturn('',-1,'上传失败');
            }

        }
     
        
    }

    /**
     * 上传任务图片
     */
    public function uploadTaskImg() {
        $input = input();
        if (empty($input['uid'])){
            return $this->ajaxReturn('',-1,'未定义uid');
        }
        if (empty($input['task_record_id'])){
            return $this->ajaxReturn('',-1,'未定义task_record_id');
        }
        if (empty($_FILES['images'])){
            return $this->ajaxReturn('',-1,'未定义images');
        }
        $data = [];
        $file = request()->file('images');// 获取表单上传图片
       
      /*  return $file;*/
        $info = $file->move('uploads/task');// 移动到框架应用根目录/uploads/task 目录下
        if ($info) {
            $path = __ROOT__ . '/' . $info->getpathName();
            $data['uid'] = $input['uid'];
            $data['task_record_id'] = $input['task_record_id'];
            $data['task_img'] = $path;
            $data['status'] = 1;
            $data['created_at'] = time();
            $id = db('upload_task_img')->insertGetId($data);
            return $this->ajaxReturn(['id'=>$id]);
        } else {
            return $this->ajaxReturn($info, -1, '上传图片失败');
        }
    }

    /**
     * 刷新当天的任务数量和任务状态
     * @return [type] [description]
     */
    public function checkRefresh($uid) {
        // $time = strtotime(date('Y-m-d'));
        // $time = strtotime(date(date('Y-m-d',$value['created_at']).' 10:50:00'));//设置失效时间
        $time = strtotime(date('Y-m-d 00:00:00'));
        $task = db('task_record')->where('uid',$uid)->where('status',1)->select();
        foreach ($task as $key => $value) {
            if($value['created_at'] < $time){
                db('task_record')->where('id',$value['id'])->update(['status'=>3]);           }
        }
        $userInfo = db('user_account')->where('id',$uid)->find();
        $task_time = $userInfo['day_time'];
        if ($task_time < $time) {
            $task_num = db('member')->where('level',$userInfo['level'])->value('task_num');//可接任务数量
            $data['day_num'] = $task_num;
            $data['day_time'] = $time;
            db('user_account')->where('id',$uid)->update($data);
        }
    }
}